<template>
  <div class="dropdown" ref="DropdownRef">
  <a href="#" class="btn btn-outline-hight dropdown-toggle" @click.prevent="toggleOpen">
   {{title}}
  </a>
  <ul class="dropdown-menu" :style="{display:'block'}" v-if="isOpen">
    <slot></slot>
  </ul>
</div>
</template>

<script lang='ts'>
import {defineComponent,ref,watch} from 'vue'

import useClickOutside from '../../hooks/useClickOutside'

export default defineComponent({
    name: 'Dropdown',
    props: {
      title: {
        type: String,
        required: true
      }
    },
    setup() {
      const isOpen = ref(false)

      const toggleOpen = () => {
        isOpen.value = !isOpen.value
      }
      
      const DropdownRef = ref<null | HTMLElement>(null)
      const isClickOutside = useClickOutside(DropdownRef)

      watch(isClickOutside,()=> {
        if (!isClickOutside.value &&isOpen.value) {
          isOpen.value = false
        }
      })

        return {
          isOpen,
          toggleOpen,
          DropdownRef
        }
    }
})
</script>

<style>

</style>